package com.oraycn.esbasic.helpers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import io.netty.buffer.ByteBuf;

/**
 * 序列化相关帮助类
 *
 */
public class SerializeHelper {
    /**
     * 序列化对象
     *
     * @param object
     * @return
     */
    public static byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            String s = "";
        } finally {
            if (oos != null) {
                try {
                    oos.flush();
                    oos.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
            if(baos!=null)
            {
                try {
                    baos.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 反序列化
     *
     * @param bytes
     * @return
     */
    public static Object deSerialize(byte[] bytes) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            Object obj = ois.readObject();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            if(bais!=null)
            {
                try {
                    bais.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 写入数据之前，先写入int长度
     *
     * @param buffer
     * @param data
     */
    public static void writeDataWithIntLen(ByteBuf buffer, byte[] data) {
        buffer.writeInt(data.length);
        buffer.writeBytes(data);
    }

    /**
     * 写入数据之前，先写入byte长度
     *
     * @param buffer
     * @param data
     */
    public static void writeDataWithByteLen(ByteBuf buffer, byte[] data) {
        buffer.writeByte(data.length);
        buffer.writeBytes(data);
    }

    /**
     * 写入数据之前，先写入数据长度，同时长度不够的情况下，补0
     *
     * @param buffer 写入的数据
     * @param data   数据长度
     */
    public static void writeDataWithByteLenFillZeo(ByteBuf buffer, byte[] data, int len) {
        buffer.writeByte(data.length);
        buffer.writeBytes(data);
        int writed = data.length;
        while (writed < len) {
            buffer.writeByte(0);
            writed++;
        }
    }

    /**
     * int len + string
     *
     * @param buffer
     * @return
     * @throws Exception
     */
    public static String readStrIntLen(ByteBuf buffer) throws IOException {
        int len = buffer.readInt();
        if (len <= 0)
            return "";
        byte[] arrayOfByte = new byte[len];
        buffer.readBytes(arrayOfByte);
        return new String(arrayOfByte, "utf-8");
    }

    /**
     * byte len + string
     *
     * @param buffer
     * @param len
     * @return
     * @throws Exception
     */
    public static String readStrByteLenFixLen(ByteBuf buffer, byte len) throws IOException {
        byte realLen = buffer.readByte();
        if (realLen <= 0) {
            return "";
        }
        byte[] data = new byte[len];
        buffer.readBytes(data);
        return new String(data, 0, realLen, "utf-8");
    }

}
